package csc.daonjpa.java.dao;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import csc.daonjpa.java.controller.EntityManagerFactoryUtil;
import csc.daonjpa.java.domain.Account;
import csc.daonjpa.java.domain.BaseInfo;
import csc.daonjpa.java.transaction.TransactionManager;
public class BaseInfoDAO {
	
	
	public boolean updateBaseInfo (Double availableAmount, Long id) {
		// Obtains entity manager object
		EntityManager entityManager = EntityManagerFactoryUtil
				.createEntityManager();

		// Obtains transaction from entity manager
		EntityTransaction entr = entityManager.getTransaction();

		// -----------Begin transaction-----------
		boolean res = true;
		try {
			entr.begin();
			String temp = "UPDATE " + BaseInfo.class.getName()
					+ " SET availableAmount = :availableAmount WHERE id = :id";
			Query query = entityManager.createQuery(temp);
			query.setParameter("availableAmount", availableAmount);
			query.setParameter("id", id);
			query.executeUpdate();
			entr.commit();
		} catch (Exception e) {
			entityManager.close();
			res = false;
		}
		// -----------End transaction-----------
		return res;
	}
	public BaseInfo getBaseInfo(Long id) {
		EntityManager em = TransactionManager.getEntityManager();
		BaseInfo baseInfo = em.find(BaseInfo.class, id);
		return baseInfo;
		
	}
}
